package com.haimi.shiguang.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haimi.shiguang.R;
import com.haimi.shiguang.framework.GlobalCache;

/**
 * Created by xiaolong on 16/6/17.
 */
public class TitleView extends RelativeLayout {

    private TextView tv_title_left;
    private TextView tv_title_middle;
    private TextView tv_title_right;


    private TitleCallBack callback;

    public TitleView(Context context) {
        super(context);
        layout(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        layout(context);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.titleview);
        String title = ta.getString(R.styleable.titleview_titleStr);
        if (title != null) {
            tv_title_middle.setText(title);
        }
        ta.recycle();

    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        layout(context);
    }


    protected void layout(Context context) {
        View.inflate(context, R.layout.view_title, this);
        initView();
    }

    public void setMiddle(String title) {
        tv_title_middle.setText(title);
    }

    public TextView getTitleLeftTv() {
        return tv_title_left;
    }

    public TextView getTitleRightTv() {
        return tv_title_right;
    }

    /**
     * @param titleCallBack 监听回调
     */
    public void setTitleCallBack(TitleCallBack titleCallBack) {
        if (titleCallBack != null)
            this.callback = titleCallBack;
    }

    /**
     * @param text             显示文字
     * @param visible          是否显示
     * @param drawable(0为null) 显示图片
     */
    public void setLeft(String text, boolean visible, int drawable
    ) {
        if (visible) {
            tv_title_left.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(text)) {
                tv_title_left.setText(text);
            }
            if (drawable != 0) {
                Drawable icon = getResources().getDrawable(drawable);
                icon.setBounds(0, 0, ScreenSizeUtil.dip2px(GlobalCache.getContext(), 16), ScreenSizeUtil.dip2px(GlobalCache.getContext(), 16));
                tv_title_left.setCompoundDrawables(icon, null, null, null);
            }
        } else {
            tv_title_left.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * @param text             显示文字
     * @param visible          是否显示
     * @param drawable(0为null) 显示图片
     */
    public void setRight(String text, boolean visible, int drawable) {
        if (visible) {
            tv_title_right.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(text)) {
                tv_title_right.setText(text);
            }
            if (drawable != 0) {
                Drawable icon = getResources().getDrawable(drawable);
                icon.setBounds(0, 0, ScreenSizeUtil.dip2px(GlobalCache.getContext(), 16), ScreenSizeUtil.dip2px(GlobalCache.getContext(), 16));
                tv_title_right.setCompoundDrawables(null, null, icon, null);
            }
        } else {
            tv_title_right.setVisibility(View.INVISIBLE);
        }
    }

    private void initView() {
        tv_title_left = (TextView) findViewById(R.id.tv_title_left);
        tv_title_middle = (TextView) findViewById(R.id.tv_title_middle);
        tv_title_right = (TextView) findViewById(R.id.tv_title_right);

        tv_title_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.callBackLeft();
                }
            }
        });
        tv_title_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.callBackRight();
                }
            }
        });
    }
}


